﻿using System;
using System.Collections.Generic;
using LogSystem;

namespace Yoozoo.Gameplay.Liberty.StreetEvent
{
    public enum TaskCounterMode
    {
        Equal = 1,
        Greater = 2,
        Less = 3,
        EqualOrGreater = 4,
        EqualOrLess = 5,
        NoLimit = 6,
    }
    
    [Serializable]
    public class StreetTaskCounter
    {
        public string counterName;
        public int count;
        public int threshold;
        public TaskCounterMode counterMode;
        
        public StreetTaskCounter(string name, int tValue, TaskCounterMode mode, int initCount = 0)
        {
            counterName = name;
            count = initCount;
            threshold = tValue;
            counterMode = mode;
        }
        
        public int AddValue(int n)
        {
            count += n;
            count = Math.Max(0, count); // 不能小于0
            StreetTaskCountUtils.OnCounterChanged?.Invoke(counterName,count);

            if (counterMode == TaskCounterMode.Equal)
            {
                if (count == threshold)
                {
                    StreetTaskCountUtils.OnCounterComplete?.Invoke(counterName,count);
                }
            }
            else if (counterMode == TaskCounterMode.Greater)
            {
                if (count > threshold)
                {
                    StreetTaskCountUtils.OnCounterComplete?.Invoke(counterName,count);
                }
            }
            else if (counterMode == TaskCounterMode.Less)
            {
                if (count < threshold)
                {
                    StreetTaskCountUtils.OnCounterComplete?.Invoke(counterName,count);
                }
            }
            else if (counterMode == TaskCounterMode.EqualOrGreater)
            {
                if (count >= threshold)
                {
                    StreetTaskCountUtils.OnCounterComplete?.Invoke(counterName,count);
                }
            }
            else if (counterMode == TaskCounterMode.EqualOrLess)
            {
                if (count <= threshold)
                {
                    StreetTaskCountUtils.OnCounterComplete?.Invoke(counterName,count);
                }
            }
            
            return count;
        }

        public int GetValue()
        {
            return count;
        }

    }
    
    public static class StreetTaskCountUtils
    {
        private static Dictionary<string, StreetTaskCounter> _streetTaskCounterMap =
            new Dictionary<string, StreetTaskCounter>();

        public static bool streetTaskCounterDirty = false;

        public static Action<string, int> OnCounterChanged;
        public static Action<string, int> OnCounterComplete;

        public static StreetTaskCounter AddCounter(string name, int threshold, TaskCounterMode counterMode, int initCount = 0)
        {
            if (string.IsNullOrEmpty(name))
            {
                Debug.LogError(LogModule.LibertyScene,"AddCounter失败，名称为空");
                return null;
            }
            
            if (_streetTaskCounterMap.ContainsKey(name))
            {
                Debug.LogError(LogModule.LibertyScene,"AddCounter失败，名称重复：" + name);
                return null;
            }

            StreetTaskCounter taskCounter = new StreetTaskCounter(name, threshold, counterMode, initCount);
            _streetTaskCounterMap[name] = taskCounter;
            return taskCounter;
        }

        public static void AddValue(string name, int n)
        {
            if (string.IsNullOrEmpty(name))
            {
                Debug.LogError(LogModule.LibertyScene,"AddValue失败 名称为空");
                return;
            }
            
            if (!_streetTaskCounterMap.ContainsKey(name))
            {
                Debug.LogError(LogModule.LibertyScene,"AddValue失败，不存在：" + name);
                return;
            }
            
            streetTaskCounterDirty = true;
            _streetTaskCounterMap[name].AddValue(n);
        }

        public static int GetValue(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                // Debug.LogError(LogModule.LibertyScene,"GetValue失败 名称为空");
                return 0;
            }
            
            if (!_streetTaskCounterMap.ContainsKey(name))
            {
                // Debug.LogError(LogModule.LibertyScene,"GetValue失败，不存在：" + name);
                return 0;
            }
            
            return _streetTaskCounterMap[name].GetValue();
        }


        public static void Clear()
        {
            _streetTaskCounterMap.Clear();
        }
    }
}